package mymac.com.weather;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mymac on 2017/3/6.
 */

public class FutureActivity extends Activity {
    private ListView listView;
    MyAdapter adapter;
    Map<String, Integer> map;
    String[] strImg = {"晴天","阴天","雨天","小雨","中雨","大雨","小雨转中雨","中雨转小雨","阵雨"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_future);
        listView = (ListView) findViewById(R.id.future_listview);
        map = new HashMap<String, Integer>();
        map.put("晴",R.drawable.qingtian);
        map.put("阴天",R.drawable.yingtian);
        map.put("雨天",R.drawable.yutian);
        map.put("小雨",R.drawable.xioayu);
        map.put("中雨",R.drawable.zhongyu);
        map.put("大雨",R.drawable.dayu);
        map.put("小雨转中雨",R.drawable.xiaoyuzhuanzhongyu);
        map.put("中雨转小雨",R.drawable.zhongyuzhuanxioayu);
        map.put("阵雨",R.drawable.zhengyu);

        adapter = new MyAdapter();
        listView.setAdapter(adapter);
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return Result.getInstance().futures.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View contentView;
            if (view == null) {
                contentView = View.inflate(FutureActivity.this, R.layout.activity_list_item, null);
            } else {
                contentView = view;
            }
            TextView week = (TextView) contentView.findViewById(R.id.future_week);
            TextView date = (TextView) contentView.findViewById(R.id.future_date);
            TextView 温度 = (TextView) contentView.findViewById(R.id.future_温度范围);
            TextView 天气 = (TextView) contentView.findViewById(R.id.future_天气);
            ImageView 天气图标 = (ImageView) contentView.findViewById(R.id.天气图标);
            week.setText(Result.getInstance().futures.get(i).getWeek());
            date.setText(Result.getInstance().futures.get(i).getDate());
            温度.setText(Result.getInstance().futures.get(i).getTemperature());
            天气.setText(Result.getInstance().futures.get(i).getWeather());
            String wea = Result.getInstance().futures.get(i).getWeather().toString().trim();
            for (int j=0; j<strImg.length; j++){
                if ((strImg[j].toString().trim()).equals(wea)){
                    System.out.println("wea:" + wea + "; str:" + strImg[j]);
                    System.out.println(Result.getInstance().futures.get(i).getWeather());
                    System.out.println("----map--->" + map.get(strImg[j]));
                    Resources res=getResources();
                    Bitmap bt = BitmapFactory.decodeResource(res,map.get(strImg[j]));
                    // 转换为BitmapDrawable对象
                    天气图标.setImageBitmap(bt);
                }
            }
            return contentView;
        }
    }
}
